package com.ibeeking.found.file.rest.controller.v1;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.file.rest.service.ICommonFileService;
import com.ibeeking.found.file.rest.service.IFilePermissionService;
import com.ibeeking.found.file.rest.service.IUserFileService;
import com.ibeeking.found.file.rest.service.util.QiwenFile;
import com.ibeeking.found.file.service.common.dos.CommonFileDO;
import com.ibeeking.found.file.service.common.dos.FilePermissionDO;
import com.ibeeking.found.file.service.common.dos.UserFileDO;
import com.ibeeking.found.file.service.common.param.CommonFileParam;
import com.ibeeking.found.file.service.common.query.CommonFilePageQuery;
import com.ibeeking.found.file.service.common.vo.CommonFileListVO;
import com.ibeeking.found.file.service.common.vo.CommonFileUserVO;
import com.ibeeking.found.file.service.common.vo.FileListVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName FriendsController
 * @Description 该接口为文件共享接口
 * @Author ibeeking
 * @Date 2021-06-02 18:15
 **/

@RestController
@Slf4j
@RequestMapping("/common/v1")
public class CommonFileController {

    public static final String CURRENT_MODULE = "文件共享";

    @Resource
    private ICommonFileService commonFileServiceImpl;

    @Resource
    private IFilePermissionService filePermissionServiceImpl;

    @Resource
    private IUserFileService userFileServiceImpl;

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "将文件共享给他人")
    @PostMapping(value = "/commonfile")
    public Boolean commonFile( @RequestBody CommonFileParam commonFileDTO) {
        CommonFileDO commonFile = new CommonFileDO();
        commonFile.setUserFileId(commonFileDTO.getUserFileId());
        commonFile.setCommonFileId(IdUtil.getSnowflakeNextId());

        commonFileServiceImpl.save(commonFile);

        List<FilePermissionDO> list = JSON.parseArray(commonFileDTO.getCommonUserList(), FilePermissionDO.class);

        List<FilePermissionDO> filePermissionList = new ArrayList<>();
       for (FilePermissionDO filePermission : list) {
           filePermission.setFilePermissionId(IdUtil.getSnowflakeNextId());
            filePermission.setCommonFileId(commonFile.commonFileId);
            filePermissionList.add(filePermission);
       }
        filePermissionServiceImpl.saveBatch(filePermissionList);

        return true;
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取共享空间的全量用户列表")
    @GetMapping(value = "/commonfileuser")
    public List<CommonFileUserVO> commonFileUserList() {
        String userId = UserUtils.getStringUserId();
        List<CommonFileUserVO> list = commonFileServiceImpl.selectCommonFileUser(userId);
        return list;
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取共享用户文件列表")
    @GetMapping(value = "/getCommonFileByUser")
    public List<CommonFileListVO> getCommonFileByUser(@LoginUser CurrentUser loginUser, @PathVariable("userId") String userId){
        List<CommonFileListVO> commonFileVo = commonFileServiceImpl.selectCommonFileByUser(userId, loginUser.getId().toString());

        return commonFileVo;

    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取共享空间中某个用户的文件列表")
    @GetMapping(value = "/commonFileList")
    public Page<FileListVO> commonFileList(@RequestBody CommonFilePageQuery pageQuery){

        CommonFileDO commonFile = commonFileServiceImpl.getById(pageQuery.getCommonFileId());
        UserFileDO userFile = userFileServiceImpl.getById(commonFile.getUserFileId());
        QiwenFile qiwenFile = new QiwenFile(userFile.getFilePath(), pageQuery.getFilePath(), true);
        Page<FileListVO> fileList = userFileServiceImpl.userFileList(userFile.getUserId(), qiwenFile.getPath(), pageQuery.getPageNum(), pageQuery.getPageSize());

        return fileList;

    }

}
